﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraLaiNhaCungCap
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {

            try
            {
                hienthi = HienThi(makho);
                XuLy();
                dgvHienThi.DataSource = hienthi;
                fix();
                if (capnhat == false)
                {
                    Them();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.TraLaiNCC thevip;
        Entities.TraLaiNCC[] hienthi;
        string makho = "";
        string tenkho = "";
        DateTime batdau;
        DateTime ketthuc;
        DateTime date;
        int i = 0;

        public frmQuanLy()
        {
            InitializeComponent();
            try
            {
                date = Entitystatic.date;
                batdau = ketthuc = date;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;

                hienthi = HienThi(makho);
                XuLy();
                dgvHienThi.DataSource = hienthi;
                fix();
            }
            catch
            {
            }
        }

        public Entities.TraLaiNCC[] HienThi(string makho)
        {
            try
            {
                Entities.TraLaiNCC[] temp = Common.StaticVariable.InterfaceObject.SelectTraLaiNCC();
                Entities.TraLaiNCC[] thevip = (Entities.TraLaiNCC[])temp.Where(p => p.MaKho == makho).ToArray();
                return thevip;
            }
            catch
            {
                return new Entities.TraLaiNCC[0];
            }
        }

        void XuLy()
        {
            hienthi = (Entities.TraLaiNCC[])(Common.StaticVariable.InterfaceObject.SelectTraLaiNCC().Where(p => p.MaKho == makho).ToArray());
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.GhiChu].Visible = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.MaHDTraLaiNCC].Visible = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TenKho].Visible = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TenNCC].Visible = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TienBoiThuong].Visible = true;
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.GhiChu].HeaderText = "Ghi Chú";
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.MaHDTraLaiNCC].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TenKho].HeaderText = "Tên Kho";
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TenNCC].HeaderText = "Tên Nhà Cung Cấp";
            dgvHienThi.Columns[Common.Constants.TraLaiNCC.TienBoiThuong].HeaderText = "Tiền Bồi Thường";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhdong)
        {
            return Common.Utilities.Check(Common.FormName.QuanlyTraLaiNCC, hanhdong);
        }
        #endregion

        #region Thêm
        void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                frmXuLy frm = new frmXuLy();
                frm.GetData = new frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region sửa
        void Sua()
        {
            try
            {
                // check per
                if (!CheckPer(2))
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (i < 0)
                    return;
                if (dgvHienThi.RowCount == 0)
                    return;
                thevip = ((Entities.TraLaiNCC[])dgvHienThi.DataSource)[i];
                // check object
                if (thevip == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                frmXuLy frm = new frmXuLy(thevip);
                frm.GetData = new frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    hienthi = HienThi(makho);
                    XuLy();
                    dgvHienThi.DataSource = hienthi;
                    fix();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }

            }
            catch
            {
            }
        }
        #endregion

        #region In
        void In()
        {
            if (i < 0)
                return;
            if (dgvHienThi.RowCount == 0)
                return;
            thevip = ((Entities.TraLaiNCC[])dgvHienThi.DataSource)[i];
            // check object
            if (thevip == null)
            {
                MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                return;
            }
            this.Enabled = false;
            Entities.NhaCungCap ncc = new Entities.NhaCungCap();
            ncc.HanhDong = "3";
            ncc.MaNhaCungCap = thevip.MaNCC;
            ncc = (Common.StaticVariable.InterfaceObject.SelectNhaCungCap(ncc))[0];
            Entities.ChiTietTraLaiNCC[] chitiet = new frmXuLy().HienThiChiTiet(thevip);
            Entities.BCTraLaiNCC[] bc = new Entities.BCTraLaiNCC[chitiet.Length];
            int sotang = 0;
            foreach (Entities.ChiTietTraLaiNCC item in chitiet)
            {

                bc[sotang] = new Entities.BCTraLaiNCC();
                bc[sotang].DonGia = Common.Utilities.FormatMoney(item.DonGia);
                bc[sotang].KhuyenMai = item.PhanTramChietKhau;
                bc[sotang].MaChungTu = item.MaHoaDon;
                bc[sotang].SoLuong = item.SoLuong.ToString();
                bc[sotang].STT = sotang + 1;
                bc[sotang].TenHang = item.TenHangHoa;
                bc[sotang].ThanhTien = double.Parse(item.ThanhTien);
                bc[sotang].Thue = item.Thue;
                sotang++;
            }
            BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(bc, thevip, ncc);
            if (frm.kiemtra)
            {
                frm.ShowDialog();
            }
            this.Enabled = true;

        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Tìm kiếm
        void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.TraLaiNCC[])hienthi.Where(p => p.MaHDTraLaiNCC.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            this.FormClosing += new FormClosingEventHandler(frmQuanLy_FormClosing);
            btnIn.Click += new EventHandler(btnIn_Click);
            btnLocDieuKien.Click += new EventHandler(btnLocDieuKien_Click);
            btnSua.Click += new EventHandler(btnSua_Click);
            btnThem.Click += new EventHandler(btnThem_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown); this.WindowState = FormWindowState.Maximized;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }
        
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion